Consider a list of lists, such as:
Z={{1,4,3},{2,1,3},{3,1,4}}
Z
is an $n \times n$ matrix (here $3 \times 3$) which represent sites. Each site has a random number. Here we have 9 sites that indexed with: 1,2,3,4,5,6,7,8,9.
Neighbors={{1, 2}, {2, 1, 3, 5, 4}, {3, 2}, {4, 7, 2}, {5, 6, 2, 8, 7, 9}, {6, 5, 9}, {7, 4, 5}, {8, 9, 5}, {9, 8, 6, 5}}
Each site have some random neighbors. In Neighbors's list first element of each member represent index of the site, and others represent neighbors of that site, for example here neighbors of site 2 are 1,3,5,4.
t={{1,0,1},{1,1,0},{0,0,0}}
Here $t$ is a list same as Z
but just have 0 at first. In every step we choose some sites and adding 1 to the neighbors of that sites (here we choosing site with index 2).
I want to write a Compile function (func) that create list t
.
also I write two function ijTok
, and kToij
to transform index of sites in neighbors list to position of that site in Z
or t
.
At last we need to use FixedPoint
:
FixedPoint[func,Z] or fixedPoint[func,z,neighbors]
I spent many hours on it and learn that Compile have some limitation. For example we not allow to use True,False value in it. this is what I write without compile I need to write same thing in Compile: Pseudo Code:
func[z_] := Module[{t= z /. x_Integer -> 0},
POSij = Position of chosen sites from Z ;
POSk = Map[ijTok, POSij];
Neighborsij = Position of neighbors of chosen sites in {i, j} form;
t += 1 just for sites that are neighbors of chosen sites ;
Return[t];
]
Mathematica form of above pseudo code:
Func[Z_] := Module[{list = Z/.x_Integer->0},
POSij =Position[Z, #& (* choosing some special sites*)];
POSk = Map[ijTok, POSij];
NeiNodeij =
Map[Map[kTOij,
Drop[Neighbors[[#]], 1]] &, POSk];
t=Map[Map[list[[Sequence @@ #]]++ &, #] &, Neighborsij], 0]
]
Here is my previous question.
t
andZ
", how is the "another list" created? I think you'd better describe your problem more clearly. $\endgroup$Compile
is the toy of experienced Mathematica user and one should gain a good enough understanding for "coding in Mathematica" before trying it. I think you'd better describe what you're trying to do in a more clear way so we can help figuring out if there's a better way to code. $\endgroup$t
described above? Then how aboutfunc[z_, nei_] := Module[{t = ConstantArray[0, Length@Flatten@z]}, t[[Rest@nei]] = 1; Partition[t, Length@z]]; func[Z, Neighbors[[2]]]
$\endgroup$